package com.office.automate.servlet.frameworks;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.office.automate.configuration.property.DatagridPageConfiguration;
import com.office.automate.dto.convertors.EmployeeDetailsConvertor;
import com.office.automate.model.employee.EmployeeSearch;

/**
 * Servlet implementation class DataGridComponentNextPreviousServlet
 */
@WebServlet("/DataGridComponentNextPreviousServlet")
public class DataGridComponentNextPreviousServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DataGridComponentNextPreviousServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String datagridPageID = (String) request.getParameter("DataGridPageID");
			String nextRowNumber = (String) request.getParameter("NextRowNumber");
			String previousRowNumber = (String) request.getParameter("PreviousRowNumber");
			RequestDispatcher dispatcher = null;
			if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.SECURITY_LEVEL))  {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/ManageSecurityLevels.jsp");				
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.BRANCH_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/ManageBranches.jsp");	
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.DIVISION_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/ManageDivisions.jsp");	
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_QUALIFICATION_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/Qualifications.jsp");	
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/Search.jsp");	
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_LEAVE_DETAILS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/ApplyForLeave.jsp");	
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.EMPLOYEE_LEAVE_APPROVE)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/ApproveLeave.jsp");
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.PUBLIC_HOLIDAYS)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/administration/AddPublicHolidays.jsp");
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.LIEU_LEAVE_REQUEST)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/RequestForLieuLeave.jsp");
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.APPROVE_LIEU_LEAVE)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/leave/ApproveLieuLeave.jsp");
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.APPROVE_OVER_TIME)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/ApproveOverTimeRequest.jsp");
			} else if (datagridPageID.equalsIgnoreCase(DatagridPageConfiguration.OVER_TIME_REQUEST)) {
				dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/ApplyForOverTime.jsp");
			}

			if (nextRowNumber != null) {
				request.setAttribute("NextDataRowNumber", nextRowNumber);
			} else if (previousRowNumber != null) {
				request.setAttribute("NextDataRowNumber", previousRowNumber);
			} else {
				throw new Exception("Invalid Operation");
			}
			EmployeeSearch employeeSearch = new EmployeeDetailsConvertor().convertEmployeeSearchRequest(request);
			request.setAttribute("EmployeeSearch", employeeSearch);
			dispatcher.forward(request, response);
		} catch(Exception e) {
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/HomePage.jsp");
			dispatcher.forward(request, response);
		}
	}
}
